package walkera.cn.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wk.smarthome.R;

import java.util.List;

import walkera.cn.UI.MaterialButtonFuck;
import walkera.cn.bean.AppDetail;
import walkera.cn.bean.AppDown;
import walkera.cn.callback.AppDetailSelectCallBack;
import walkera.cn.callback.AppTypeSelectCallBack;


public class AppDetailAdapter extends RecyclerView.Adapter {

    private List<AppDetail> book ;
    private AppDetailSelectCallBack selectCallBack ;

    public AppDetailAdapter(Context context, List<AppDetail> book  , AppDetailSelectCallBack selectCallBack) {
        this.book = book;
        this.selectCallBack = selectCallBack ;
        this.setHasStableIds(true); //

    }

    @Override
    public ChooseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = null;
        view = LayoutInflater.from(context).inflate(R.layout.item_appdetail, parent, false);
        return new ChooseHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        AppDetail bean = book.get(position);
        final ChooseHolder chooseHolder = (ChooseHolder)holder;
        chooseHolder.item_txt.setText( bean.getAppName()+"--> "+bean.getInfo()  );

    }



    public void cleanData(){
        book.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return book==null? 0: book.size();
    }



//    private int selectIndex = -1 ;
    @Override
    public long getItemId(int position) {
        return position;
    }


        private class ChooseHolder extends RecyclerView.ViewHolder {

        private TextView item_txt;
         private ChooseHolder(View itemView){
            super(itemView);
            item_txt =  itemView.findViewById(R.id.appDetail);
            View.OnClickListener clickListener = new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    int mSelectedItem = getAdapterPosition();
                    selectCallBack.setSelectedCallBack(book.get(mSelectedItem)  ,mSelectedItem);
                    notifyDataSetChanged() ;
                }
            };
            item_txt.setOnClickListener(clickListener);

        }
    }


}
